package com.mmall.util;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

/**
 * By guoguo
 */
public class DateTimeUtil {
    public  static  final String STANDARD_FORMAT="yyyy-MM-dd HH:mm:ss";
    /**
     * 字符串时间转时间
     * @param dataTimeStr
     * @param formatStr
     * @return
     */
    public static Date strToDate(String dataTimeStr,String formatStr){
        DateTimeFormatter dateTimeFormatter=DateTimeFormat.forPattern(formatStr);
        DateTime dateTime=dateTimeFormatter.parseDateTime(dataTimeStr);
        return  dateTime.toDate();
    }

    /**
     * 时间转字符串
     * @param date
     * @param formatStr
     * @return
     */
    public static String dateToStr(Date date,String formatStr){
        if (date==null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime=new DateTime(date);
        return dateTime.toString(formatStr);
    }

    public static Date strToDate(String dataTimeStr){
        DateTimeFormatter dateTimeFormatter=DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime=dateTimeFormatter.parseDateTime(dataTimeStr);
        return  dateTime.toDate();
    }


    public static String dateToStr(Date date){
        if (date==null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime=new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);
    }
    public  static void main(String[] args){
       System.out.println(dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
    }

}
